<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" href="/js/layui/css/layui.css" media="all">
    <script src="/js/layui/layui.js"></script>
    <script src="/js/jquery-1.11.0.min.js"></script>
    <script src="admin/js/token.js"></script>
</head>
<body>
<div class="layui-inline">
    <label class="layui-form-label">到期时间：</label>
    <div class="layui-input-inline">
        <input type="text" class="layui-input" id="test1" placeholder="年-月-日 时:分:秒">
    </div>
</div>
<br/>
<br/>
<br/>
<div class="layui-input-block">
    <button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="LAY-user-back-search" id="btnSure">
        确定
    </button>
</div>
</body>
<script type="text/javascript">
    var layer;
    layui.use('layer', function () {
        layer = layui.layer;
    });
    var s = layui.data("s");
    layui.use('laydate', function () {
        var $ = layui.$;
        var laydate = layui.laydate;
        var nowTime = new Date().valueOf();
        var max = null;
        var start = laydate.render({
            elem: '#test1',
            type: 'datetime',

            change: function (value, date, endDate) {
                var timestamp2 = Date.parse(new Date(value));
                timestamp2 = timestamp2 / 1000;
            }
        });

    });

    $('#btnSure').click(function () {
        var shiajin = $('#test1').val();
        var dqTime = Date.parse(shiajin);
        var nowTime = Date.parse(new Date());

        function dateFtt(fmt, date) {
            var o = {
                "M+": date.getMonth() + 1,
                "d+": date.getDate(),
                "h+": date.getHours(),
                "m+": date.getMinutes(),
                "s+": date.getSeconds(),
            };
            if (/(y+)/.test(fmt))
                fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
            for (var k in o)
                if (new RegExp("(" + k + ")").test(fmt))
                    fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            return fmt;
        }

        var beginTime = dateFtt("yyyy-MM-dd hh:mm:ss", new Date());
        var url = "/admin/terminal-key/usePasswordKey";
        var nowDate = new Date();
        var nextDate = new Date(nowDate.getTime() + 24 * 60 * 60 * 1000);
        if (dqTime < nowTime) {
            layer.msg("设置时间不能小于当前时间", {icon: 2});
            return;
        }

        //获取父级页面的值
        function getQueryVariable(variable) {
            var query = window.location.search.substring(1);
            var vars = query.split("&");
            for (var i = 0; i < vars.length; i++) {
                var pair = vars[i].split("=");
                if (pair[0] == variable) {
                    return pair[1];
                }
            }
            return (false);
        }

        var id = getQueryVariable("id");
        var bh=getQueryVariable('bh');
        var adminId=layui.data("local_store").token;
        var url = "/admin/terminal-key/usePasswordKey";
        var senddata = {
            terminal_key_id: s.terminal.terminal_key_id,
            terminal_id: id,
            terminal_key_value: s.terminal.terminal_key_value,
            terminal_key_begin_time: beginTime,
            terminal_key_end_time: shiajin,
            terminal_room:"钟点房",
            terminal_bh:bh,
            admin_id:adminId
        };
        var callback = function (res) {
            if (res.code == 200) {
                layer.msg("设置成功", {icon: 1});
                setTimeout(function () {
                    layer.close(layer.index);
                    window.parent.parent.location.reload();
                }, 2000)
                return;
            }
            if(res.code==403){
                layer.msg("权限不足", {icon: 2});
            }
            layer.msg("设置失败", {icon: 2});
        }
        $.get(url, senddata, callback);
    })
</script>
</html>
